package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.sup.domain.ProductCategories;
import com.jf3q.sup.dto.ProductCategoriesDto;
import com.jf3q.sup.vo.ResultVo;
import com.jf3q.sup.service.ProductCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author: luxiang
 * @CreateTime:2024/3/25 10:45
 * 商品类别层
 */
@RestController
@RequestMapping("/productCategories")
@CrossOrigin
public class ProductCategoriesController {
    @Autowired
    ProductCategoriesService productCategoriesService;

    /**
     * 根据条件获取商品分类分页列表
     * @param categoriesDto 商品分类信息DTO
     * @return ResultVo 包含商品分类分页列表的响应结果
     */
    @PostMapping("/getPage")
    public ResultVo commodityAll(@RequestBody ProductCategoriesDto categoriesDto){
        try {
            Page<ProductCategories> list = productCategoriesService.commodityAllPage(categoriesDto);
            return ResultVo.success("所有商品分类",list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    //添加商品类型
    @PostMapping("/create")
    public ResultVo create(@RequestBody ProductCategories productCategories){
        try {
            long count = productCategoriesService.count(new LambdaQueryWrapper<ProductCategories>().eq(ProductCategories::getName, productCategories.getName()));
            if (count != 0){
                return ResultVo.error("商品分类已存在");
            }
            productCategories.setCreatedAt(new Date());
            productCategories.setUpdatedAt(new Date());
            productCategoriesService.save(productCategories);
            return ResultVo.success("新增成功",null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 创建商品分类
     * @param productCategories 待创建的商品分类信息
     * @return ResultVo 包含创建结果的响应
     */
    @PutMapping("/update")
    public ResultVo update(@RequestBody ProductCategories productCategories){
        try {
            if (productCategories.getId() != null){
                List<ProductCategories> list = productCategoriesService.list(new LambdaQueryWrapper<ProductCategories>().eq(ProductCategories::getName, productCategories.getName()));
                for (ProductCategories categories : list) {
                    if (productCategories.getName().equals(categories.getName())) {
                        productCategories.setUpdatedAt(new Date());
                        productCategoriesService.update(productCategories,new LambdaQueryWrapper<ProductCategories>().eq(ProductCategories::getId,productCategories.getId()));
                        return ResultVo.success("修改成功",null);
                    }
                }
            }
            return  ResultVo.error("修改失败");
        } catch (Exception e) {
            throw new RuntimeException("类别名称已存在");
        }
    }

    /**
     * 根据ID删除商品分类
     * @param id 待删除商品分类的ID
     * @return ResultVo 包含删除操作结果的响应
     */
    @DeleteMapping("/delete/{id}")
    private ResultVo delete(@PathVariable Integer id){
        try {
            productCategoriesService.removeById(id);
            return ResultVo.success("删除成功",null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取所有商品分类列表
     * @return ResultVo 包含所有商品分类列表的响应
     */
    @GetMapping("/all")
    public ResultVo all(){
        try {
            List<ProductCategories> list = productCategoriesService.list();
            return ResultVo.success("展示在下拉框的所有商品类别展示",list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
